library(sf) #for polygons
library(tidyverse) #for data wrangling
library(tmap) #for interactive maps
library(janitor) #for cleaning names
#create list of years
kern_field_years <- c(2015:2019)

#loop through read-in process
for (i in kern_field_years) {
  filename <- paste0("kernfields_", i)
  wd <- paste0("data/kernfields/kern", i, ".shp")
  assign(filename, read_sf(wd))
}

#select region (based off of farmer)
kern_subset <- kernfields_2019 %>% 
  filter(AGENT == "McMANUS/WILSON,MICHELE/AARIN")

#crop several other years
kern_15_crop <- st_crop(kernfields_2015, kern_subset)

kern_16_crop <- st_crop(kernfields_2016, kern_subset)

kern_17_crop <- st_crop(kernfields_2017, kern_subset)

kern_18_crop <- st_crop(kernfields_2018, kern_subset)

kern_19_crop <- st_crop(kernfields_2019, kern_subset)

#bind together
sample_bind <- rbind(kern_17_crop, kern_18_crop, kern_19_crop) %>% 
  st_make_valid() %>% 
  clean_names() %>%
  distinct() %>% 
  mutate(year = lubridate::year(dt_act))

#merge permit site and year, then select only pmt_site and year 
sample_clean <- sample_bind %>% 
  mutate(pmt_site_year = str_c(year, "_", pmt_site)) %>% 
  dplyr::select(pmt_site_year) %>% 
  st_make_valid()

#st_write(sample_clean, here("data", "shapefiles_written", "sample_clean.shp"))

I took the shapefile sample_clean.shp and uploaded it to mapshaper’s online interface (https://mapshaper.org/). Then I entered in the command line $ mosaic and downloaded results as a shapefile, as seen in the next code chunk.

This thread could potentailly improve workflor https://github.com/mbloch/mapshaper/issues/353

#this workflow appears to work
sample_mosiac <- st_read("data/mapshaper_outputs/sample_clean/sample_clean.shp")
## Reading layer `sample_clean' from data source 
##   `/Users/irisfoxfoot/Desktop/Kern RA/kernRA_coding_projects/data/mapshaper_outputs/sample_clean/sample_clean.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 817 features and 1 field
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 6159540 ymin: 2352351 xmax: 6187962 ymax: 2386672
## Projected CRS: NAD83 / California zone 5 (ftUS)
sample_join <- st_intersection(st_buffer(sample_mosiac, dist = -1), sample_clean)

sample_pivot <- sample_join %>% 
  separate(pmt_site_year, into = c("year", "pmt_site"), sep = "_") %>% 
  select(-FID) %>% 
  pivot_wider(names_from = year, values_from = pmt_site) %>%
  st_sf() %>% 
  st_buffer(dist = 0) %>% 
  st_make_valid()

#wrire to csv to preserve listed attributes
write_csv(sample_pivot, "data/shapefiles_written/2005-2020_pivoted.csv")

PMT_SITE Pivot Table

tmap_mode("view")

tm_shape(sample_pivot) +
  tm_polygons(id = "pmt_site_year") +
  tmap_options(check.and.fix = TRUE)

Original Data

tmap_mode("view")

tm_shape(sample_bind) +
  tm_polygons(id = "pmt_site") +
  tm_facets(by = "year")